<?
require_once('functions.php');

function GetCustomers()
{
        $conn = GetConnection();
        return $conn->query('SELECT * FROM Customers');
}

function GetMethod($id, $table)
{
            $conn = GetConnection();
        $sql = "SELECT * FROM $table WHERE Customers_id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $pay = $results;
        $conn->close();
        return $pay;
}
function GetOrderItems($id)
{
            $conn = GetConnection();
        $sql = "SELECT * FROM OrderItems WHERE Orders_id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $pay = $results;
        $conn->close();
        return $pay;
}
function GetItem($id)
{
            $conn = GetConnection();
        $sql = "SELECT * FROM Item WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $pay = $results->fetch_assoc();
        $conn->close();
        return $pay;
}
function GetCustomer($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM Customers WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}
function GetInfoType($id)
{
        $conn = GetConnection();
        $sql = "SELECT * FROM ContactInfoType WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $row = $results->fetch_assoc();
        $conn->close();
        return $row;
}

function Delete($row)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM Customers "
                .       "WHERE id=$row[id]";
        $result1 = $conn->query($sql);
    //echo $sql;
        $error = $conn->error;
        echo $error;
    $conn->close();     
        return $error == '' ? true : array('SQL Error' => $error);
}

function BlankUser()
{
        return array('FirstName'=>null,'LastName'=>null,'Keyword_id'=>null,'created_at'=>null,'updated_at'=>null,'id'=>null);
}

function SaveCustomer(&$row)
{
        $conn = GetConnection();
        $row2 = EscapeRow($row, $conn);
        if(empty($row['id']))
        {
                $sql = 	"INSERT INTO Customers (FirstName, LastName, Address1, Address2, City, State, Country, ZipCode, AccessLevel_id,created_at)
                VALUES ('$row[FirstName]', '$row[LastName]', '$row[Address1]', '$row[Address2]',
                       '$row[City]', '$row[State]', '$row[Country]', '$row[ZipCode]', '1', NOW()) " ;
        }else{
                $sql =	"UPDATE Customers "
                .       "Set FirstName='$row2[FirstName]', LastName='$row2[LastName]',
                                 created_at='$row2[created_at]', updated_at='$row2[updated_at]', Address1='$row2[Address1]',
                                 Address2='$row2[Adress2]', City='$row2[City]', State='$row2[State]', 
                                 Country='$row2[Country]', ZipCode='$row2[ZipCode]' "
                .       "WHERE id=$row2[id] ";  
        }
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        if(empty($row['id']) && empty($error))
        {
                $row['id'] = $conn->insert_id;
        }
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}
function DeleteCustomer($id)
{
        $conn = GetConnection();
        $sql =  "DELETE FROM 2012UG_Users WHERE id=$id";
        $results = $conn->query($sql);
        //echo $sql;
        $error = $conn->error;
        $conn->close();
        
        return $error == '' ? true : array('SQL Error' => $error);
}
function ValidateUser($row)
{
        $errors = array();
        if(empty($row['FirstName'])) $errors['FirstName'] = 'First Name is required';
        if(empty($row['LastName'])) $errors['LastName'] = 'Last Name is required';
        if(!is_numeric($row['Keyword_id'])) $errors['Keyword_id'] = 'Keyword_id must be a number';
        if(empty($row['Keyword_id'])) $errors['Keyword_id'] = 'Keyword_id is required';
		if(empty($row['created_at'])) $errors['created_at'] = 'created_at is required';
        if(empty($row['updated_at'])) $errors['updated_at'] = 'Last Name is required';
        if(!is_numeric($row['Keyword_id'])) $errors['Keyword_id'] = 'Keyword_id must be a number';
        if(empty($row['Keyword_id'])) $errors['Keyword_id'] = 'Keyword_id is required';
        
        return count($errors) == 0 ? true : $errors;
}

